package stu.day9_day18;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class train1 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String[] str1 = sc.nextLine().split(" ");

        int len = Integer.parseInt(str1[0]);
        HashSet<String> set = new HashSet<>();
        for(int i=0;i<len;i++){
            set.add(str1[i+1]);
        }

        ArrayList<String> list = new ArrayList<>();
        int num = Integer.parseInt(sc.nextLine());
        for(int i=0;i<num;i++){
            String[] str = sc.nextLine().split(" ");
            String name = str[0];
            int length = Integer.parseInt(str[1]);
            for(int j=0;j<length;j++){
                if(set.contains(str[j+2])){
                    list.add(name);
                }
            }
        }

        System.out.println(list.size());
        Collections.sort(list);
        for(String s:list){
            System.out.print(s+" ");
        }





    }

}
